//
//  ReferDetailViewController.m
//  MMLOOK
//
//  Created by dllo on 15/8/27.
//  Copyright (c) 2015年 武俊娥. All rights reserved.
//

#import "ReferDetailViewController.h"
#import "AsynConnect.h"
#import "ReferTextCostumCell.h"
#import "UIImageView+WebCache.h"
#import "ReferPictureCell.h"
#import "ReferModel.h"
#import "MBProgressHUD+MJ.h"

@interface ReferDetailViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>


@property (nonatomic, retain)NSMutableArray *pictureArray;

@property (nonatomic, retain)UICollectionView *collection;

@property (nonatomic, retain)UIImageView *imageView;

@property (nonatomic, retain) UICollectionViewFlowLayout *layout;

@property (nonatomic, retain) MBProgressHUD *mbProView;
@end

@implementation ReferDetailViewController

- (void)dealloc
{
    [_DetailDic release];
    [_collection release];
    
    [_pictureArray release];
    [_imageView release];
    [_mbProView release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.DetailDic = [NSMutableDictionary dictionary];
        
        self.pictureArray = [NSMutableArray array];
       
    }
    return self;
}
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:253 / 255.0 green:180/255.0 blue:83/255.0 alpha:1];
    self.navigationItem.title = @"详情";
    [self Connection];
   
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self creatCollection];
    
}


- (void)creatCollection
{
    
    self.imageView = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:self.imageView];
    self.layout = [[UICollectionViewFlowLayout alloc] init];
    NSLog(@"2");
    _layout.itemSize = CGSizeMake(290 * OffWidth, 450 * OffHeight);
    _layout.sectionInset = UIEdgeInsetsMake(50 * OffHeight, 30 * OffWidth, 50 * OffHeight, 30 * OffWidth);
    _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    _layout.minimumInteritemSpacing = 30 * OffHeight;
    _layout.minimumLineSpacing = 30 * OffWidth;
    
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.view.frame.size.height) collectionViewLayout:_layout];
    
    _collection.dataSource = self;
    _collection.delegate = self;
    
    [_collection registerClass:[ReferTextCostumCell class] forCellWithReuseIdentifier:@"重用池"];
    [_collection registerClass:[ReferPictureCell class] forCellWithReuseIdentifier:@"图片"];
    _collection.backgroundColor = [UIColor clearColor];
    _collection.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width + 60, 0);
    _collection.showsHorizontalScrollIndicator = NO;
    _collection.layer.cornerRadius = 10 * OffWidth;
    [self.view addSubview:_collection];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    return _pictureArray.count;
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
   
    if (indexPath.section == 0) {
        
        ReferTextCostumCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"重用池" forIndexPath:indexPath];
        _layout.itemSize = CGSizeMake(290, [ReferTextCostumCell hightWithtext:cell.detailLable.text]);
        NSLog(@"1");
        

        [_imageView sd_setImageWithURL:[NSURL URLWithString:self.model.bkg_path]];
        
        cell.titlelable.text = self.model.title;
        
        cell.titlelable.numberOfLines = 0;
        
        
        cell.detailLable.text = self.model.desc;
        cell.detailLable.numberOfLines = 0;
        
        
        return cell;
    }else{
    
    

    ReferPictureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"图片" forIndexPath:indexPath];
    _layout.itemSize = CGSizeMake(290 * OffWidth, 450 * OffHeight);
    ReferModel *model = [_pictureArray objectAtIndex:indexPath.row];
    [cell.showImageView sd_setImageWithURL:[NSURL URLWithString:model.photo_path]placeholderImage:[UIImage imageNamed:@"home.jpg"]];
    
    cell.piclable.text = model.subject_desc;
    
    return cell;
    }
    
    return nil;
}


- (void)Connection
{
    [self createMbProView];
    [AsynConnect asynConnectWithUrlstr:[NSString stringWithFormat:@"http://www.hunliji.com/subjects/%ld.json",self.model.id ] parmater:nil block:^(NSData *data) {
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
       //NSMutableDictionary *little = [dic objectForKey:@"subject"];
        
        NSMutableArray *arr = [dic objectForKey:@"products"];
        
        for (NSMutableDictionary *produce in arr) {
            ReferModel *model = [[[ReferModel alloc] init] autorelease];
            [model setValuesForKeysWithDictionary:produce];
            [self.pictureArray addObject:model];
        }
        [_collection reloadData];
        [_mbProView hide:YES];
        
    }];
}


#warning 菊花效果
- (void)createMbProView
{
    self.mbProView = [[[MBProgressHUD alloc] initWithView:self.view] autorelease];
    _mbProView.mode = MBProgressHUDModeIndeterminate;// 样式 菊花
    _mbProView.labelText = @"加载中. . .";
    [_mbProView show:YES];
    [self.view addSubview:_mbProView];
    [_mbProView hide:YES afterDelay:20];
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
